﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Buca.Application.aBigTime.Model.BusinessObjects.Cash;
using Buca.Application.aBigTime.Presenter.Cash.ReceiptVoucher;
using Buca.Application.aBigTime.View.Cash;
using Buca.Application.aBigTime.WindowsForm.FormBase;
using Buca.Application.aBigTime.WindowsForm.FormBase.PropertyGrid;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;

namespace Buca.Application.aBigTime.WindowsForm.FormBusiness
{
    public partial class FrmReceiptVoucherDetail : FrmBaseVoucherDetail, IReceiptVoucherView
    {
        private ReceiptVoucherPresenter _receiptVoucherPresenter;

        #region ReceiptVoucher

        /// <summary>
        /// Gets or sets the account identifier.
        /// </summary>
        /// <value>
        /// The account identifier.
        /// </value>
        public int ReceiptVoucherID { get; set; }
        /// <summary>
        /// Gets or sets the code.
        /// </summary>
        /// <value>
        /// The code.
        /// </value>
        public string Code
        {
            get { return txtRefNo.Text; }
            set { txtRefNo.Text = value; }
        }
        /// <summary>
        /// Gets or sets the reference date.
        /// </summary>
        /// <value>
        /// The reference date.
        /// </value>
        public string RefDate
        {
            get { return dateRefDate.EditValue != null ? DateTime.Parse(dateRefDate.EditValue.ToString()).ToShortDateString() : DateTime.Now.ToShortDateString(); }
            set { dateRefDate.EditValue = value.ToString().Equals("01/01/0001") ? DateTime.Now.ToShortDateString() : value; }
        }
        /// <summary>
        /// Gets or sets the create by.
        /// </summary>
        /// <value>
        /// The create by.
        /// </value>
        public string CreateBy
        {
            get { return txtCreateBy.Text; }
            set { txtCreateBy.Text = value; }
        }
        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>
        /// The description.
        /// </value>
        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }
        /// <summary>
        /// Gets or sets the total amount.
        /// </summary>
        /// <value>
        /// The total amount.
        /// </value>
        public decimal TotalAmount {
            get { return (decimal)grdView.Columns["Amount"].SummaryItem.SummaryValue; } 
            set { } 
        }
        /// <summary>
        /// Gets or sets the receipt voucher details.
        /// </summary>
        /// <value>
        /// The receipt voucher details.
        /// </value>
        public IList<ReceiptVoucherDetailModel> ReceiptVoucherDetails
        {
            get
            {
                var receiptVoucherDetails = new List<ReceiptVoucherDetailModel>();
                if (grdMain.DataSource != null && grdView.RowCount > 0)
                {
                    for (int i = 0; i < grdView.RowCount; i++)
                    {
                        if (grdView.GetRow(i) != null)
                        {
                            receiptVoucherDetails.Add(new ReceiptVoucherDetailModel()
                            {
                                ItemName = grdView.GetRowCellValue(i, "ItemName").ToString(),
                                Quantity = (int)grdView.GetRowCellValue(i, "Quantity"),
                                Amount = (decimal)grdView.GetRowCellValue(i, "Amount")
                            });
                        }
                    }
                }
                return receiptVoucherDetails.ToList();
            }
            set
            {
                bindingSourceDetail.DataSource = value == null ? new List<ReceiptVoucherDetailModel>() : value;
                grdView.PopulateColumns(value);

                //define control that binding to grid
                var repositoryItemAmount = new RepositoryItemTextEdit();
                repositoryItemAmount.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
                repositoryItemAmount.Mask.EditMask = "#,###,###,###";

                var repositoryItemQuantity = new RepositoryItemSpinEdit();
                repositoryItemQuantity.MinValue = 1;
                repositoryItemQuantity.MaxValue = 999999;
                repositoryItemQuantity.EditMask = "N00";

                ColumnsCollection.Add(new XtraColumn { ColumnName = "ReceiptVoucherDetailID", ColumnVisible = false });
                ColumnsCollection.Add(new XtraColumn { ColumnName = "ReceiptVoucherID", ColumnVisible = false });
                ColumnsCollection.Add(new XtraColumn { ColumnName = "ItemName", ColumnCaption = "Tên TS", ColumnPosition = 1, ColumnVisible = true, ColumnWith = 100 });
                ColumnsCollection.Add(new XtraColumn { ColumnName = "Quantity", ColumnCaption = "Số lượng", ColumnPosition = 2, ColumnVisible = true, ColumnWith = 300, RepositoryControl = repositoryItemQuantity });
                ColumnsCollection.Add(new XtraColumn { ColumnName = "Amount", ColumnCaption = "Số tiền", ColumnPosition = 3, ColumnVisible = true, ColumnWith = 200, RepositoryControl = repositoryItemAmount, IsSummnary = true, DisplayFormat = "Tổng= {0:#,###,###,###,###,###}" });
            }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="FrmReceiptVoucherDetail"/> class.
        /// </summary>
        public FrmReceiptVoucherDetail()
        {
            InitializeComponent();
            _receiptVoucherPresenter = new ReceiptVoucherPresenter(this);
        }
        /// <summary>
        /// Adds the reference no.
        /// </summary>
        protected override void AddRefNo()
        {
            txtRefNo.Text = RefNo;
        }
        /// <summary>
        /// Focuses the control.
        /// </summary>
        protected override void InitControls()
        {
            txtCreateBy.Focus();
        }
        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected override void InitData()
        {
            var receiptVoucherID = KeyValue == null ? 0 : int.Parse(KeyValue);
            _receiptVoucherPresenter.Display(receiptVoucherID);
        }
        /// <summary>
        /// Handles the ValidateRow event of the grdView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs"/> instance containing the event data.</param>
        private void grdView_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            var amountCol = grdView.Columns["Amount"];
            var amount = (decimal)grdView.GetRowCellValue(e.RowHandle, amountCol);
            if (amount < 0)
            {
                e.Valid = false;
                grdView.SetColumnError(amountCol, "Không được nhập giá < 0");
            }
        }
        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected override int SaveData()
        {
            return _receiptVoucherPresenter.Save();
        }
    }
}